package client_controllers;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;

import client_controllers.ProductController.AddItemListener;
import client_controllers.ProductController.RemoveItemListener;
import client_gui.DefineCategoryGUI;
import client_gui.DefineTypeGUI;
import client_gui.ProductGUI;
import client_logic.CcrmClient;
import client_logic.dbHandler;
import entity.Boss;
import entity.Product;
import entity.Type;

/**
 * @class DefineTypeController ,in this class the marketing manager define new type.
 * @param DefineTypeGUI links to GUI.
 * @param EmployeeLoginController links to employee controller. 
 * @param Boss is used as an instance, to send objects to DB and allow changes in the lists.
 * @param MarketingManagerController links to marketing manager controller.
 * @param type is the class that save all the info of the type.
 */
public class DefineTypeController implements dbHandler,Serializable {
	
	private DefineTypeGUI dtg;
	private EmployeeLoginController elc;
	private MarketingManagerController mmc;
	private DefineTypeController dtc;
	private Type type;
	private Boss b;

	public DefineTypeController(DefineTypeGUI dtg,MarketingManagerController mmc
			,EmployeeLoginController elc){
		this.dtc= this;
		this.dtg=dtg;
		this.elc = elc;
		this.mmc=mmc;
		this.dtg.getBtnAddType().addActionListener(new AddTypeListener());
		this.dtg.getBtnRemoveType().addActionListener(new RemoveTypeListener());
		this.dtg.getBtnBack().addActionListener(new backListener());
		getTypeCombo();
		
	}
	
	public class RemoveTypeListener implements ActionListener{
		public void actionPerformed (ActionEvent ev){
			// get product names into
			type = new Type(dtg.getTypes().getSelectedItem().toString());
			

			dtc.setB(new Boss(type,"deleteType","DefineTypeController"));
			dtc.Delete();
			
		}
	}
	/** 
	 * listener for the "add type" button 
	 **/
	public class AddTypeListener implements ActionListener{
		public void actionPerformed (ActionEvent ev){
			try{
				String str= dtg.getNewType().getText();
				type = new Type(str);
				dtc.setB(new Boss(type,"addType","DefineTypeController"));
				dtc.Add();
			}catch(Exception ex ){
				dtg.getLblMsg().setText("missing fields");
				dtg.getLblMsg().setVisible(true);
			}

			
		}
	}
	/** 
	 * listener for the "Back" button 
	 **/
	public class backListener implements ActionListener{
		public void actionPerformed (ActionEvent ev){
			dtg.dispose();
			mmc.getMmg().setVisible(true);
			
		}
	}
	
	/**
	 * This function getTypeCombo fill the type combo with values
	 */
	private void getTypeCombo() {
		// TODO Auto-generated method stub
		this.setB(new Boss(null,"getTypes","DefineTypeController"));
		this.Search();
	}

	@Override
	public void Add() {
		try{CcrmClient.inet.setCurrCon(this);
		CcrmClient.inet.sendToServer(b);
			
		}catch (IOException ex){
			ex.printStackTrace();
		}
		
	}

	@Override
	public void Search() {
		try{CcrmClient.inet.setCurrCon(this);
		CcrmClient.inet.sendToServer(b);
			
		}catch (IOException ex){
			ex.printStackTrace();
		}
		
		
	}

	@Override
	public String Delete() {
		try{CcrmClient.inet.setCurrCon(this);
		CcrmClient.inet.sendToServer(b);
			
		}catch (IOException ex){
			ex.printStackTrace();
		}
		
		return null;
	}

	/**
	 * the function HandleMsg get massage from server
	 * @param msg it item from the DB Usually comes as Boss
	 */
	@Override
	public void HandleMsg(Object msg) {
		this.setB((Boss) msg);
		if(this.getB().getCommand().equals("getTypes")){
		ArrayList <String> types ;
		types= (ArrayList<String>) this.getB().getObj();
		for(String i :types)
			dtg.getTypes().addItem(i); 
			//System.out.println(i);
		}
		if(this.getB().getCommand().equals("deleteType")){
			if(this.getB().getMsg().equals("type deleted successefully")){
				
				dtg.getLblMsg().setText("type deleted successefully");
				dtg.getLblMsg().setForeground(Color.green);
				dtg.getLblMsg().setVisible(true);
				dtg.getTypes().removeAllItems();
				this.getTypeCombo();
			}
			else {
				dtg.getLblMsg().setText("could not delete this type");
				dtg.getLblMsg().setForeground(Color.red);
				dtg.getLblMsg().setVisible(true);
			}
					

	} 
		
		  		if(this.getB().getCommand().equals("addType")){
				if(this.getB().getMsg().equals("type added successefully")){
					
					dtg.getLblMsg().setText("type added successefully");
					dtg.getLblMsg().setForeground(Color.green);
					dtg.getLblMsg().setVisible(true);
					dtg.getTypes().removeAllItems();
					this.getTypeCombo();

				}
				else {
					dtg.getLblMsg().setText("could not add this type");
					dtg.getLblMsg().setForeground(Color.red);
					dtg.getLblMsg().setVisible(true);
				}
						

		}
		 
		
	}

	@Override
	public void update() {
		// TODO Auto-generated method stub
		
	}

	public DefineTypeGUI getDtg() {
		return dtg;
	}

	public void setDtg(DefineTypeGUI dtg) {
		this.dtg = dtg;
	}

	public EmployeeLoginController getElc() {
		return elc;
	}

	public void setElc(EmployeeLoginController elc) {
		this.elc = elc;
	}

	public MarketingManagerController getMmc() {
		return mmc;
	}

	public void setMmc(MarketingManagerController mmc) {
		this.mmc = mmc;
	}

	public DefineTypeController getDtc() {
		return dtc;
	}

	public void setDtc(DefineTypeController dtc) {
		this.dtc = dtc;
	}

	public Type getType() {
		return type;
	}

	public void setType(Type type) {
		this.type = type;
	}

	public Boss getB() {
		return b;
	}

	public void setB(Boss b) {
		this.b = b;
	}
	

}
